package com.multiplayer.dao.impl;

import java.sql.Connection;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.multiplayer.dao.IReservationDao;
import com.multiplayer.dao.ISeatDao;
import com.multiplayer.dao.IUserDao;
import com.multiplayer.entity.Reservation;
import com.multiplayer.entity.Seat;
import com.multiplayer.entity.StudyRoom;
import com.multiplayer.entity.User;
import com.multiplayer.tools.DBTools;
import com.multiplayer.tools.ITransaction;
import com.multiplayer.tools.PageData;



/**
 * @author 曹倩
 *
 */
public class ReservationDaoImpl implements IReservationDao {

	@Override
	public List<Reservation> getAllReservation(String userid) {
		String sql="select * from reservation where userid like ?";
		return (List<Reservation>)DBTools.exQuery(sql, Reservation.class, "%"+userid+"%");
	}

	@Override
	public List<Reservation> getReservation(int resid) {
		String sql="select * from reservation where resid like ?";
		return (List<Reservation>)DBTools.exQuery(sql, Reservation.class, "%"+resid+"%");
	}
	
	
	@Override
	public boolean delReservation(String userid) {
		String sql="delete from reservation where userid=?";
		return DBTools.exUpdate(sql,userid)>0;
	}

	@Override
	public boolean addReservation(String eseatno, Time restime, Time resetime) {
		// TODO Auto-generated method stub
		String sql = "insert into reservation(eseatno,restime,resetime) values(?,?,?)";
		int n = DBTools.exUpdate(sql, eseatno,restime,resetime);
		return n>0;
	}

	/**
	 * 通过模糊查询分页显示自习室信息
	 */
	@Override
	public PageData<Reservation> getReservationByPage(int page, int pageSize, String keywords) {
		// TODO Auto-generated method stub
		PageData<Reservation> pd = DBTools.exQueryByPage("select * from reservation where resid like ? or userid like ? or eseatno like ?"
				, Reservation.class
				,page, pageSize,keywords,"%" + keywords + "%",keywords);
		return pd;
	}

	@Override
	public boolean rejectReservation(int resid) {
		// TODO Auto-generated method stub
		String sql = "delete from reservation where resid=?";
		IReservationDao reservationDao=new ReservationDaoImpl();
		List<Reservation> reservationList=reservationDao.getReservation(resid);
		
		String Eseatno=reservationList.get(0).getEseatno();
		String sql1 = "update  seat  set estatus =? where eseatno=?";

		int result = (Integer)DBTools.transaction(new ITransaction() {
			@Override
			public Object execute(Connection conn) {
				// TODO Auto-generated method stub
				int n1 = 0;
				int n2 = 0;
				try {
					n1 = DBTools.exUpdate(sql, conn,resid);
					n2 = DBTools.exUpdate(sql1, conn,"未预约",Eseatno);
					System.out.println("result:"+n1*n2);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					throw new RuntimeException("保存失败，事务回滚!", e);
				}
				return n1*n2;
			}
		});
		return result>0;
	}

	@Override
	public boolean acceptReservation(int resid) {
		// TODO Auto-generated method stub
		IReservationDao reservationDao=new ReservationDaoImpl();
		List<Reservation> reservationList=reservationDao.getReservation(resid);
		
		String Eseatno=reservationList.get(0).getEseatno();
		String sql1 = "update  seat  set estatus =? where eseatno=?";

		int result = (Integer)DBTools.transaction(new ITransaction() {
			@Override
			public Object execute(Connection conn) {
				// TODO Auto-generated method stub
				int n2 = 0;
				try {
					n2 = DBTools.exUpdate(sql1, conn,"已预约",Eseatno);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					throw new RuntimeException("保存失败，事务回滚!", e);
				}
				return n2;
			}
		});
		return result>0;
	}

	@Override
	public void autoProcessReservation() throws ParseException {
		// TODO Auto-generated method stub
		IReservationDao reservationDao=new ReservationDaoImpl();
		//获得所有预约
		List<Reservation> reservationList=reservationDao.getAllReservation("");
		for (Reservation reservation : reservationList) {
			IUserDao userDao=new UserDaoImpl();
			List<User> userList=userDao.queryUserById(reservation.getUserid());
			//获得用户信誉度
			Double Credibility=userList.get(0).getCredibility();
			
			//获得预约状态
			ISeatDao seatDao=new SeatDaoImpl();
			List<Seat> seatList=seatDao.querySeatByKeywords(reservation.getEseatno());
			String Restatus=seatList.get(0).getEstatus();
			//获得现在时间
			String DateString=reservation.getResetime()+"";
			SimpleDateFormat s=new SimpleDateFormat("HH:mm:ss");
			Date endTime=s.parse(DateString.toString());
			Date now=new Date();
			long timeGaps=0;
			timeGaps=endTime.getTime()-now.getTime();
			//如果用户预约时间到期则结束预约
			if(timeGaps>0)
			{
				if(reservationDao.rejectReservation(reservation.getResid()))
				{System.out.println("如果用户预约时间到期则结束预约");}
				else{System.out.println("3");}
			}
			//如果用户信誉度大于等于80且预约状态为未预约则预约成功
			else if(Credibility>=80&&Restatus.equals("未预约"))
			{
				if(reservationDao.acceptReservation(reservation.getResid()))
				{System.out.println("信誉度大于等于80且预约状态为未预约则预约成功");}
				else{System.out.println("1");}
			}
			//如果用户信誉度小于80且预约状态为未预约则预约失败
			else if(Credibility<80&&Restatus.equals("未预约"))
			{
				if(reservationDao.rejectReservation(reservation.getResid()))
				{System.out.println("如果用户信誉度小于80且预约状态为未预约则预约失败");}
				else{System.out.println("2");}
			}

		}
	}


}